<?php
/**
 * (Amazon)fba转款报告
 * (amazon-model)
 */
namespace App\Models\SalesReport;

use App\Models\Basics\SysPlatAccount;
use App\Models\Basics\AmazonCountry;
use App\Models\Basics\SysCountry;
use App\Models\Basics\SysArea;
use Illuminate\Database\Eloquent\Model;
class FBADeposit extends Model
{
    protected $connection = 'mysql_amazon';

    protected $table = 'fba_settlement_deposit';

    protected $guarded = [];

    public static $_EURCountryId = 99;//欧元国家ID

    protected static $_EURCountry = 'EU';//欧元国家

    public static function countryOptions($where = [])
    {
        $country = AmazonCountry::options($where)->toArray();
        return $country + [self::$_EURCountryId => self::$_EURCountry];
    }

    public static function getCountryById($countryId)
    {
        if($countryId == self::$_EURCountryId)return self::$_EURCountry;
        return SysCountry::getCountryById($countryId);
    }

    public static $_statusOption = [
        '1' => '最新',
    ];

    public static $_status = [
        '0' => '待判断',
        '1' => '最新',
        '2' => '历史',
    ];

    protected static $_marketplace =
        [
            'amazon.de' => 'DE',
            'amazon.es' => 'ES',
            'amazon.fr' => 'FR',
            'amazon.it' => 'IT',
            'amazon.nl' => 'NL',
            'amazon.pl' => 'PL',
            'amazon.se' => 'SE',
            'amazon.tr' => 'TR',
            'amazon.co.uk' => 'UK',
        ];

    public static function getEUCountryId($marketplaceName)
    {
        if(!$marketplaceName)return 0;
        $_marketplaceName = strtolower($marketplaceName);
        if(array_key_exists($_marketplaceName,self::$_marketplace)){
            $country = self::$_marketplace[$_marketplaceName];
            return SysCountry::getIdByCountry($country);
        }else{
            return 0;
        }
    }

    public static function headersField(){
        $fields = [];
        $fields['settlement_id'] = ['title' => '结算ID','sumField' => false];
        $fields['account'] = ['title' => '账号','sumField' => false];
        $fields['area'] = ['title' => '站点','sumField' => false];
        $fields['country'] = ['title' => '国家','sumField' => false];
        $fields['deposit_date'] = ['title' => '转款时间','sumField' => false];
        $fields['currency'] = ['title' => '币种','sumField' => false];
        $fields['rate_odds'] = ['title' => '汇率','sumField' => false];
        $fields['total_amount'] = ['title' => '转款金额','sumField' => false];
        $fields['total_amount_rmb'] = ['title' => '转款金额RMB','sumField' => true];
//        $fields['pre_reserve_amount_balance'] = ['title' => '上期结余','sumField' => false];
//        $fields['pre_reserve_amount_balance_rmb'] = ['title' => '上期结余RMB','sumField' => true];
        $fields['cur_reserve_amount'] = ['title' => '当期结余','sumField' => false];
        $fields['cur_reserve_amount_rmb'] = ['title' => '当期结余RMB','sumField' => true];
        $fields['settlement_sdate'] = ['title' => '结算开始时间','sumField' => false];
        $fields['settlement_edate'] = ['title' => '结算截止时间','sumField' => false];
        return $fields;
    }

    public static function getDownloadData($re){
        foreach ($re as $row){
            $temp = [
                $row->settlement_id,
                SysPlatAccount::getAccountById($row->account_id),
                SysArea::getSysAreaById($row->area_id),
                self::getCountryById($row->country_id),
                $row->deposit_date,
                $row->currency,
                $row->rate_odds,
                $row->total_amount,
                round($row->rate_odds * $row->total_amount,3),
//                $row->pre_reserve_amount_balance,
//                round($row->rate_odds * $row->pre_reserve_amount_balance,3),
                $row->cur_reserve_amount,
                round($row->rate_odds * $row->cur_reserve_amount,3),
                $row->settlement_sdate,
                $row->settlement_edate,
            ];
            yield $temp;
        }
    }
}
